package cn.car.schedule;

import cn.car.biz.carAccess.event.VehicleRecordPutGovEvent;
import cn.car.domains.entity.VehicleEntryExitInfoEntity;
import cn.car.domains.entity.VehicleEntryExitLedgerInfoEntity;
import cn.car.domains.enums.UploadGovStatusEnum;
import cn.car.service.VehicleEntryExitLedgerInfoService;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import jakarta.annotation.Resource;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class VehicleLedgerSchedule {

    @Resource
    private VehicleEntryExitLedgerInfoService vehicleEntryExitLedgerInfoService;

    @Resource
    private ApplicationEventPublisher publisher;

    @Scheduled(fixedRate = 30,timeUnit = TimeUnit.MINUTES,initialDelay = 0)
    public void compensateVehicleLedgerInfoUploadStatus() {

        DateTime startTime = DateUtil.offset(new Date(), DateField.HOUR_OF_DAY, -24);
        DateTime endTime = DateUtil.offset(new Date(), DateField.MINUTE, -30);
        List<VehicleEntryExitLedgerInfoEntity> vehicleEntryExitLedgerInfoEntityList = vehicleEntryExitLedgerInfoService.compensateVehicleLedgerInfoUploadStatus(startTime, endTime);


        for (VehicleEntryExitLedgerInfoEntity vehicleEntryExitLedgerInfoEntity : vehicleEntryExitLedgerInfoEntityList) {

            if (vehicleEntryExitLedgerInfoEntity.getEntryUploadStatus() != null
                    && !UploadGovStatusEnum.SUCCESS.name().equals(vehicleEntryExitLedgerInfoEntity.getEntryUploadStatus())) {


                //VehicleRecordPutGovEvent plateDetectResultEvent = new VehicleRecordPutGovEvent(this, context);
                //publisher.publishEvent(plateDetectResultEvent);
            }

            if (vehicleEntryExitLedgerInfoEntity.getExitUploadStatus() != null
                    && !UploadGovStatusEnum.SUCCESS.name().equals(vehicleEntryExitLedgerInfoEntity.getExitUploadStatus())) {
                //VehicleRecordPutGovEvent plateDetectResultEvent = new VehicleRecordPutGovEvent(this, context);
                //publisher.publishEvent(plateDetectResultEvent);
            }


        }
    }
}
